import cv2
import numpy as np
import matplotlib.pyplot as plt
import cmath


def DegradeImage(F, a, b, T):
	degradedF = F.copy()
	height, width = F.shape
	for i in range(height):
		for j in range(width):
			degradedF[i,j] = F[i,j]*CalculateDegradationFormula(i, j, a, b, T)

	return degradedF

def CalculateDegradationFormula(u, v, a, b, T):
	w = (u*a + v*b)*cmath.pi
	if w==0:
		return 0
	else:
		return T*cmath.sin(w)*(cmath.e**(-1.0j*w))/w


def EnlargeImage(Img):
	PaddedImg = np.empty([rawHeight*2, rawWidth*2], dtype=np.uint8)
	for i in range(rawHeight):
		PaddedImg[i, 0:rawWidth] = rawImg[i, :]

	return PaddedImg



if __name__ == '__main__':
	rawImg = cv2.imread('./img/fig6.jpg',0)
	rawHeight, rawWidth = rawImg.shape
	plt.subplot(241),plt.imshow(rawImg, cmap = 'gray')
	plt.title('Input Image'), plt.xticks([]), plt.yticks([])

<<<<<<< HEAD
	impulse = np.zeros()

	enlargedImg = EnlargeImage(rawImg)
=======
	# enlargedImg = EnlargeImage(rawImg)
>>>>>>> 97ffddeb944c750c3fe5711356c697fab51969fe

	# #plt.subplot(242),plt.imshow(enlargedImg, cmap = 'gray')
	# #plt.title('Large Image'), plt.xticks([]), plt.yticks([])

	# #PaddedImg = np.empty([rawHeight*2, rawWidth*2], dtype=np.uint8)
	# #for i in range(rawHeight):
	# #	PaddedImg[i, 0:rawWidth] = rawImg[i, :]

<<<<<<< HEAD
	#cv2.namedWindow("Padded Img", cv2.CV_WINDOW_AUTOSIZE)
	#cv2.imshow("Padded Img", enlargedImg)

	# f = np.fft.fft2(rawImg)
	f = np.fft.fft2(enlargedImg)
=======
	# cv2.namedWindow("Padded Img", cv2.CV_WINDOW_AUTOSIZE)
	# cv2.imshow("Padded Img", enlargedImg)

	# f = np.fft.fft2(enlargedImg)
	f = np.fft.fft2(rawImg)

>>>>>>> 97ffddeb944c750c3fe5711356c697fab51969fe
	myfshift = np.fft.fftshift(f)

	magnitude_spectrum = 20*np.log(np.abs(myfshift))
	plt.subplot(242),plt.imshow(magnitude_spectrum, cmap = 'gray')
	plt.title('Magnitude Spectrum'), plt.xticks([]), plt.yticks([])

	# degradedf = DegradeImage(myfshift, 0.1, 0.1, 1)
	# # de_magnitude_spectrum = 20*np.log(np.abs(degradedf))

	mHeight, mWidth = myfshift.shape
	degradationMask = np.zeros(myfshift.shape, dtype=complex)
	for i in range(mHeight):
		for j in range(mWidth):
			degradationMask[i, j] = CalculateDegradationFormula(i, j, 0.1, 0.1, 1)
	
	mask_magnitude_spectrum = 20*np.log(np.abs(degradationMask))
	plt.subplot(243),plt.imshow(mask_magnitude_spectrum, cmap = 'gray')
	plt.title('Mask Mag Spectrum'), plt.xticks([]), plt.yticks([])

	result = myfshift.copy()
	for i in range(mHeight):
		for j in range(mWidth):
			result[i,j] = myfshift[i, j]*degradationMask[i,j]
<<<<<<< HEAD
=======
	
	#ishift_degradedf = np.fft.ifftshift(result)
	#degradedPaddedImg = np.fft.ifft2(ishift_degradedf).real
	degradedPaddedImg = np.fft.ifft2(result).real
>>>>>>> 97ffddeb944c750c3fe5711356c697fab51969fe

	# result_magnitude_spectrum = 20*np.log(np.abs(result))
	# plt.subplot(247),plt.imshow(result_magnitude_spectrum, cmap = 'gray')
	# plt.title('Result Magnitude Spectrum'), plt.xticks([]), plt.yticks([])

	# # plt.subplot(122),plt.imshow(de_magnitude_spectrum, cmap = 'gray')
	# # plt.title('Degraded Magnitude Spectrum'), plt.xticks([]), plt.yticks([])

	ishift_degradedf = np.fft.ifftshift(result)
	# ishift_degradedf = np.fft.ifftshift(myfshift)
	degradedPaddedImg = np.fft.ifft2(ishift_degradedf).real

	plt.subplot(244),plt.imshow(degradedPaddedImg, cmap = 'gray')
	plt.title('Result Image'), plt.xticks([]), plt.yticks([])

	plt.show()

	cv2.waitKey(0)
	cv2.destroyAllWindows()
